//
//  ClassifyViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "ClassifyViewController.h"

@interface ClassifyViewController ()

@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"分类";
    
    
    // 瀑布流类
    UICollectionViewFlowLayout *classifyFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的大小
    classifyFlowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3.7, self.view.frame.size.height / 5);
    // 设置最小列间距
    classifyFlowLayout.minimumInteritemSpacing = 8;
    // 设置最小行间距
    classifyFlowLayout.minimumLineSpacing = 30;
    // 设置底部区域大小
    classifyFlowLayout.footerReferenceSize = CGSizeMake(49, 49);
    // 设置item 上左下右的边距大小
    classifyFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    // 集合视图
    self.ClassifyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64) collectionViewLayout:classifyFlowLayout];
    self.ClassifyCollectionView.backgroundColor = [UIColor whiteColor];
    // 指定代理人
    self.ClassifyCollectionView.dataSource = self;
    self.ClassifyCollectionView.delegate = self;
    
    [self.view addSubview:self.ClassifyCollectionView];
    
    
    // 注册cell
    [self.ClassifyCollectionView registerClass:[ClasstfyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    // 菊花类
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    
    self.HUD.labelText = @"加载中";
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
        [self doTask];
    } completionBlock:^{
        
        
    }];
    
    
    // 利用封装的AFN解析
    // 初始化数组
    self.allClassifyArr = [NSMutableArray array];
    
    [GetAFN downLoadBlock:@"http://api.hoto.cn/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1405673489437&vc=57&vn=3.7.0&loguid=0&deviceid=haodou864052025053998&uuid=c4affd54-af74-4a3d-8058-d434982df3c3&channel=360_v370&method=Search.getCateListV3&virtual=" block:^(id result) {
        
        NSMutableDictionary *classifyDic = result;
        NSMutableDictionary *resultDic = [classifyDic objectForKey:@"result"];
        NSMutableArray *classifyArr = [resultDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in classifyArr) {
            Classify *classify = [[Classify alloc] init];
            [classify setValuesForKeysWithDictionary:dic];
            // 将请求下来的数据存到数组中
            [self.allClassifyArr addObject:classify];
            
        }
        // 刷新collectionView
        [self.ClassifyCollectionView reloadData];
        
        
        // 加载完成,取消菊花
        [self.HUD removeFromSuperview];

        
    }];
    
}
// 菊花类自定义方法(最长显示时间10秒)
- (void)doTask
{
    sleep(10);
}
// 设置item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allClassifyArr.count;
}
// 显示item内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClasstfyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    Classify *classify = [self.allClassifyArr objectAtIndex:indexPath.row];
    // 赋值
    cell.cateLable.text = classify.Cate;
    
    // 给自定义cell中的imageView添加固定的图片
    NSString *classifyName = [NSString stringWithFormat:@"classifyImage%ld", indexPath.row];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:classifyName ofType:@"png"];
    UIImage *classifyImage = [[UIImage alloc] initWithContentsOfFile:filePath];
    cell.classifyImageView.image = classifyImage;
    
    
    return cell;
}
// 点击item跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyListViewController *classifyListVC = [[ClassifyListViewController alloc] init];
    
    Classify *classify = [self.allClassifyArr objectAtIndex:indexPath.row];
    // 将cate的值传到下一个页面
    classifyListVC.cateListName = classify.Cate;
    // 因为这一层的中的的接口有下一层cell要显示的内容, 需要加将获取到得数组传给下一页面
    classifyListVC.allClassifyListArr = classify.Tags;
    
    
    // push到下一个页面
    [self.navigationController pushViewController:classifyListVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
